package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.clientCenter.queryBean.UserInfoQueryBean;
import com.koron.css2.common.dto.AttachmentDto;
import com.koron.css2.common.service.AbstractExportExcel;
import com.koron.css2.newProjectCenter.bean.PjMeter;
import com.koron.css2.newProjectCenter.bean.query.PjMeterQuery;
import com.koron.css2.newProjectCenter.mapper.PjMeterMapper;
import com.koron.css2.newProjectCenter.vo.PjMeterExportVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.CommonUtils;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.css2.systemManage.mapper.OrgUserMapper;
import com.koron.util.DateUtils;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

public class PjMeterInstallInfoExport extends AbstractExportExcel {

    @Override
    public AttachmentDto getAttachment(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        PjMeterQuery pjMeterQuery = JsonUtils.objectToPojo(req.getData(), PjMeterQuery.class);
        PjMeterMapper pjMeterMapper = factory.getMapper(PjMeterMapper.class);
        UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);
        OrgUserMapper orgUserMapper = factory.getMapper(OrgUserMapper.class);

        List<PjMeterExportVO> pjMeterExportVOList = new ArrayList<>();

        // 1. 查出所有的立户信息
        List<PjMeter> pjMeterList = pjMeterMapper.getList(pjMeterQuery);
        // 2. 查出对应的临时用户信息
        List<String> userInfoTempIdList = pjMeterList.stream().map(PjMeter::getUserInfoTempId).collect(Collectors.toList());
        UserInfoQueryBean userInfoQueryBean = new UserInfoQueryBean();
        userInfoQueryBean.setIdList(userInfoTempIdList);
        List<UserBean> userBeanList = userInfoMapper.getList(userInfoQueryBean, UserBean.USERTEMP);
        // 3. pjMeter:setupMeterAddr,meterBjmsName,meterSite,meterBjxsName,meterBuilderName
        pjMeterList.forEach(pjMeter -> {
            PjMeterExportVO pjMeterExportVO = new PjMeterExportVO();
            // 3.1 获取对应的userBean
            int i = pjMeterList.indexOf(pjMeter);
            UserBean userBean = userBeanList.get(i);
            // 3.2 setupMeterAddr
            pjMeterExportVO.setSetupMeterAddr(userBean.getSetupMeterAddr());
            // 3.3 meterBjmsName
            pjMeterExportVO.setMeterBjmsName(Tools.getDicNameByCodeAndValue(factory, "BJMS", userBean.getMeterBjms()));
            // 3.4 meterSite
            pjMeterExportVO.setMeterSite(userBean.getMeterSite());
            // 3.5 meterBjxsName
            pjMeterExportVO.setMeterBjxsName(Tools.getDicNameByCodeAndValue(factory, "BJXS", userBean.getMeterBjxs()));
            // 3.6 meterBuilderName
            String meterBuilderName = Tools.getUserNameByAccount(userBean.getMeterBuilder(),orgUserMapper);
            pjMeterExportVO.setMeterBuilderName(meterBuilderName);
            // 3.7 planMeterInstallDate
            Date planMeterInstallDate = pjMeter.getPlanMeterInstallDate();
            if (planMeterInstallDate != null) {
                pjMeterExportVO.setPlanMeterInstallDate(DateUtils.parseDate(planMeterInstallDate,"yyyy-MM-dd"));
            }
            pjMeterExportVOList.add(pjMeterExportVO);
        });
        // 4. 导出excel
        AttachmentDto attachment = new AttachmentDto();
        attachment.setBtlTemplate("PjMeterInstallInfoExport.btl");
        attachment.setFileTemplate("PjMeterInstallInfoExport.xlsx");
        attachment.setData(pjMeterExportVOList);
        attachment.setFileType("excel");
        attachment.setFilename("工程报装-工程施工派单信息" + CommonUtils.getCurrentDate());
        return attachment;
    }
}
